package demo6;
class Money implements Cloneable{
    double money=12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{
    int age;
    Money m=new Money();

    public Student(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp=(Student) super.clone();
        tmp.m=(Money) this.m.clone();
        return tmp;
    }
}

public class Text {
    public static void main(String[] args) throws CloneNotSupportedException{
        Student student1=new Student(10);
        Student student2=(Student) student1.clone();
        //1.所有的类都继承object，因为object父类下面的方法为protected clone，父类，子类的包不同，不能直接访问，需要用到super
        //2, Student student2=(Student) student1.clone();为向下转型，所以要强制类型转换
        //3，异常问题
        //4，必须实现cloneable接口（空接口、标记接口），只要实现这个克隆接口，证明这个类才能被实现
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("--------------");
        student2.m.money=100;
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }
}
